﻿/*************************************************
作者: Plane
邮箱: 1785275942@qq.com
功能: 角色创建与选择

           ~~获取更多教学资讯~~
//----------------*----------------\\
        教学官网：www.qiqiker.com
        官方微信服务号: qiqikertuts
        Plane老师微信: PlaneZhong
\\----------------*----------------//
*************************************************/

using PEProtocol;
using UnityEngine;
using System.Collections.Generic;

public class CreateWnd : WindowRoot {
    //-------------Declaration----------------//
    private AcctSys acctSys;
    public Transform itemRoot;
    int selectIndex = -1;
    List<RoleData> roleDatas;
    //-------------Flow Functions-------------//
    protected override void InitWnd() {
        base.InitWnd();

        acctSys = root.acctSys;
        root.evtSvc.AddListener(EvtID.OnRoleDataUpdated, OnRoleDataUpdated);

        roleDatas = acctSys.RoleDatas;
        OnRoleDataUpdated(roleDatas, null);
    }

    //-------------Core Functions-------------//
    void OnRoleDataUpdated(object a, object b) {
        roleDatas = (List<RoleData>)a;
        for (int i = 0; i < 3; i++) {
            Transform item = GetTrans(itemRoot, $"Item_{i}");
            if (roleDatas != null && i < roleDatas.Count) {
                SetActive(item);
                RoleData roleData = roleDatas[i];
                SetText(GetText(item, "bgName/name"), roleData.nickName);
            }
            else {
                SetActive(item, false);
            }
        }

        if (roleDatas != null && roleDatas.Count > 0) {
            if (selectIndex == -1) {
                ClickRoleItem(0);
            }
            else {
                ClickRoleItem(selectIndex);
            }
        }
    }

    //-------------Tool Functions-------------//
    public void ClickRoleItem(int index) {
        selectIndex = index;
        for (int i = 0; i < 3; i++) {
            Transform item = GetTrans(itemRoot, $"Item_{i}");
            if (i == index) {
                SetActive(GetTrans(item, "bg_selected"));
            }
            else {
                SetActive(GetTrans(item, "bg_selected"), false);
            }
        }
    }
    public void ClickEnterBtn() {
        audioSvc.PlayUIAudio("loginBtnClick");

        if (selectIndex == -1) {
            this.Error("无角色数据");
            return;
        }

        //TODO 
    }
}